package com.mofeng.myshop.domain.vo;

import com.mofeng.myshop.domain.po.AddressMap;
import com.mofeng.myshop.domain.po.Commodity;
import com.mofeng.myshop.domain.po.OrderFrom;
import com.mofeng.myshop.domain.po.UserBase;
import com.mofeng.myshop.domain.vo.base.BaseVo;
import com.mofeng.myshop.domain.vo.user.UserVo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class OrderFromVo extends BaseVo {
    @Schema(description = "订单状态 0 暂存购物车 1 交易失败 2 交易完成 3 等待发货 4等待签收")
    private String state;

    @Schema(description = "金额")
    private String money;

    @Schema(description = "数量")
    private Integer count;

    @Schema(description = "质量")
    private Integer multiplier;

    @Schema(description = "等级")
    private Integer level;

    @Schema(description = "是否是蓝图 0 没有 1 有")
    private boolean isBluePic;

    @Schema(description = "收货地址")
    private String addressId;

    @Schema(description = "其他信息")
    private String otherMsg;

    @Schema(description = "商品信息，用JSON存(当商品删除时，会将商品信息存进来)")
    private String commodityMsg;

    @Schema(description = "商品id")
    private String commodityId;

    @Schema(description = "虚拟删除状态， 0 存在  1删除")
    private String delete;

    @Schema(description = "付款用户ID")
    private String buyUserId;

    @Schema(description = "收货 用户ID")
    private String toUserId;

    private CommodityVo commodity;

    private AddressMapVo address;

    private UserVo toUser;

    private UserVo buyUser;

    public OrderFromVo(OrderFrom orderFrom) {
        super(orderFrom);
        this.state = orderFrom.getState();
        this.money = orderFrom.getMoney();
        this.count = orderFrom.getCount();
        this.multiplier = orderFrom.getMultiplier();
        this.isBluePic = "1".equals(orderFrom.getIsBluePic());
        this.level = orderFrom.getLevel();
        this.addressId = orderFrom.getAddressId() == null ? null : orderFrom.getAddressId().toString();
        this.otherMsg = orderFrom.getOtherMsg();
        this.commodityMsg = orderFrom.getErrorMsg();
        this.commodityId = orderFrom.getCommodityId() == null ? null : orderFrom.getCommodityId().toString();
        this.delete = orderFrom.getDelete();
        this.buyUserId = orderFrom.getBuyUserId() == null ? null : orderFrom.getBuyUserId().toString();
        this.toUserId = orderFrom.getToUserId() == null ? null : orderFrom.getToUserId().toString();
        Commodity commodityEntity = orderFrom.getCommodity();
        if (commodityEntity != null) {
            this.commodity = new CommodityVo(commodityEntity);
            this.commodity.setId(this.commodityId);
        }
        AddressMap addressMap = orderFrom.getAddressMap();
        if (addressMap != null) {
            this.address = new AddressMapVo(addressMap);
        }
        UserBase toUser = orderFrom.getToUser();
        if (toUser != null) {
            this.toUser = new UserVo(toUser);
            this.toUser.setId(this.toUserId);
        }
        UserBase buyUser = orderFrom.getBuyUser();
        if (buyUser != null) {
            this.buyUser = new UserVo(buyUser);
            this.buyUser.setId(this.buyUserId);
        }
    }

    public static List<OrderFromVo> change(Collection<OrderFrom> orderFromCollection) {
        if (orderFromCollection == null || orderFromCollection.isEmpty()) {
            return null;
        }
        return orderFromCollection.stream().map(OrderFromVo::new).collect(Collectors.toList());
    }
}
